/*
 * Copyright (C) Antonio Ramon Morcillo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package soft.xitxarello.su.activities;

import soft.xitxarello.su.R;
import soft.xitxarello.su.data.App;
import soft.xitxarello.su.fragments.DetailFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

/**
 * Main activity launched first in the application.
 * 
 * @see soft.xitxarello.su.activities.BaseActivity
 */
public class DetailActivity extends BaseActivity {

    // Detailed Application.
    private App app;

    /**
     * Overrides a superclass method. Called when the activity is starting.
     * 
     * @param savedInstanceState A mapping from String values to various
     *            Parcelable types.
     * @see android.os.Bundle
     * @see soft.xitxarello.su.activities.BaseActivity#onCreate(Bundle
     *      savedInstanceState)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // Parent method.
        super.onCreate(savedInstanceState);

        // Inflate xml Resource. /res/layout/main_activity.xml
        setContentView(R.layout.detail_activity);

        // First time we get application from Extras in other cases from saved
        // instance.
        if (savedInstanceState == null) {
            Bundle b = getIntent().getExtras();
            app = b.getParcelable("app");
        } else {
            app = savedInstanceState.getParcelable("app");
        }

        // Get the fragment manager and transaction.
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        // Create the application detail fragment and replace their content.
        DetailFragment detail = DetailFragment.newInstance(app);
        ft.replace(R.id.detail_activity_layout, detail);
        ft.commit();

    }

    /**
     * Overrides a superclass method. Called to retrieve per-instance state from
     * an activity before being killed so that the state can be restored in
     * <code>onCreate(Bundle)</code> or
     * <code>onRestoreInstanceState(Bundle)</code> (the Bundle populated by this
     * method will be passed to both).
     * 
     * @param savedInstanceState A mapping from String values to various
     *            Parcelable types.
     * @see android.os.Bundle
     * @see soft.xitxarello.su.activities.BaseActivity#onSaveInstanceState
     */
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);

        // Save app into the mapping of savedInstanceState.
        savedInstanceState.putParcelable("app", app);

    }
}
